var waybill_history ={
	init:function(){
		var that = this;
		that.dateInit();

		var bid_no = $.trim($("#HBid_No").val()),
            ordernumber = $.trim($("#HOrderNumber").val()),
            load_state = $("#HState option:selected").attr("value"),
            state_bid_time1 = $.trim($("#Hconsignee_time1").val()) == '' ? '': new Date($.trim($("#Hconsignee_time1").val())).getTime(),
            state_bid_time2 = $.trim($("#Hconsignee_time2").val()) == '' ? '':new Date($.trim($("#Hconsignee_time2").val())).getTime(),
            end_bid_time1 = $.trim($("#HPickup_time1").val()) == '' ? '':new Date($.trim($("#HPickup_time1").val())).getTime(),
            end_bid_time2 = $.trim($("#HPickup_time2").val()) =='' ? '':new Date($.trim($("#HPickup_time2").val())).getTime();


		$.ajax({
			url:COMMONURL+"/api/bidload/GetPageList?token="+$.cookie('token'),
			type:"post",
			data:{
				bid_no:bid_no,
                ordernumber:ordernumber,
                load_state:load_state,
                consignee_time1:state_bid_time1,
                consignee_time2:state_bid_time2,
                pickup_time1:end_bid_time1,
                pickup_time2:end_bid_time2,
				"page":1,
				"pagesize":20,
				state:20
			},
			dataType:"json"
		}).done(function(redata){
			if (redata.recode == '0000' && redata.mod.Items.length>0) {
                var compiled = _.template($("#waybillHistoryTpl").html());
                var odata = redata.mod.Items;
                // alert(that.format(odata[0].start_bid_time))
                for (var i = 0, length = odata.length; i < length; i++) {
                    odata[i].consignee_time = that.format(odata[i].consignee_time);
                    odata[i].pickup_time=(odata[i].pickup_time!=null && odata[i].pickup_time!='')?odata[i].pickup_time.replace('T',' ').replace('Z',''):'';
                }
           
               $("#waybillHistoryCont").html(compiled(redata.mod));
               $("#wayHisdemo").show().jqPaginator({
                    //totalPages: Math.ceil(redata.mod.TotalNum/20),
                    totalPages: redata.mod.TotalPageCount,
                    visiblePages: 5,
                    currentPage: 1,
                    first: '<li class="first"><a href="javascript:void(0);">首页<\/a><\/li>',
                    prev: '<li class="prev"><a href="javascript:void(0);"><i class="arrow arrow2"><\/i>上一页<\/a><\/li>',
                    next: '<li class="next"><a href="javascript:void(0);">下一页<i class="arrow arrow3"><\/i><\/a><\/li>',
                    last: '<li class="last"><a href="javascript:void(0);">末页<\/a><\/li>',
                    page: '<li class="page"><a href="javascript:void(0);">{{page}}<\/a><\/li>',
                    onPageChange: function(num,type) {
                       
                        $("#whpage").val(num);
                        if(type =='change'){
                            that.doPage();
                        }
                    }
                });
     
            }
            else{

                var compiled = _.template($("#waybillHistoryTpl").html());
                var odata = redata.mod.Items;
                $("#waybillHistoryCont").html(compiled(redata.mod));
                $("#wayHisdemo").hide();
            }
			});

		$("#HQUery").click(function(){
			that.doSearch();
		});
	},
	
    dateInit: function() {
        $('.datepicker').datetimepicker({
            language: 'zh-CN',
            weekStart: 1,
            todayBtn: 1,
            autoclose: 1,
            todayHighlight: 1,
            startView: 2,
            minView: 2,
            forceParse: 0,
            format: 'yyyy-mm-dd hh:ii'
        });
    },
	format:function(nS){
            //return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/,' ');
            if(!nS) return null;
            var date = new Date(nS*1000);
            var year = date.getFullYear();
            var month = date.getMonth()+1;
            var d = date.getDate();
            var hours = date.getHours();
            var minutes = date.getMinutes();
            var seconds = date.getSeconds();
            return year+'-'+month+'-'+d+' '+hours+':'+minutes;
    },
    doSearch:function(){
    	var that = this;
       	var bid_no = $.trim($("#HBid_No").val()),
            ordernumber = $.trim($("#HOrderNumber").val()),
            load_state = $("#HState option:selected").attr("value"),
            state_bid_time1 = $.trim($("#Hconsignee_time1").val()) == '' ? '': new Date($.trim($("#Hconsignee_time1").val())).getTime(),
            state_bid_time2 = $.trim($("#Hconsignee_time2").val()) == '' ? '':new Date($.trim($("#Hconsignee_time2").val())).getTime(),
            end_bid_time1 = $.trim($("#HPickup_time1").val()) == '' ? '':new Date($.trim($("#HPickup_time1").val())).getTime(),
            end_bid_time2 = $.trim($("#HPickup_time2").val()) =='' ? '':new Date($.trim($("#HPickup_time2").val())).getTime();


    	var page = $.trim($("#whpage").val());	
    	$.ajax({
    		url:COMMONURL+"/api/bidload/GetPageList?token="+ $.cookie('token'),
    		type:"post",
    		data:{
	    		bid_no:bid_no,
                ordernumber:ordernumber,
                load_state:load_state,
                consignee_time1:state_bid_time1,
                consignee_time2:state_bid_time2,
                pickup_time1:end_bid_time1,
                pickup_time2:end_bid_time2,
				"page":1,
				"pagesize":20,
				state:20
			},
			dataType:"json"
    	}).done(function(redata){
    		if (redata.recode == '0000' && redata.mod.Items.length>0) {
                var compiled = _.template($("#waybillHistoryTpl").html());
                var odata = redata.mod.Items;
                // alert(that.format(odata[0].start_bid_time))
                for (var i = 0, length = odata.length; i < length; i++) {
                    odata[i].start_bid_time = that.format(odata[i].start_bid_time);
                    odata[i].end_bid_time = that.format(odata[i].end_bid_time);
                   
                     odata[i].consignee_time=that.format(odata[i].consignee_time);
                }
                $("#waybillHistoryCont").html(compiled(redata.mod));

            

                
            }else{
                var compiled = _.template($("#waybillHistoryTpl").html());
                var odata = redata.mod.Items;
                $("#waybillHistoryCont").html(compiled(redata.mod));
                $("#wayHisdemo").hide();
            }
    	});

    },
    doPage:function(){
        var that = this;
       	var bid_no = $.trim($("#HBid_No").val()),
            ordernumber = $.trim($("#HOrderNumber").val()),
            load_state = $("#HState option:selected").attr("value"),
            state_bid_time1 = $.trim($("#Hconsignee_time1").val()) == '' ? '': new Date($.trim($("#Hconsignee_time1").val())).getTime(),
            state_bid_time2 = $.trim($("#Hconsignee_time2").val()) == '' ? '':new Date($.trim($("#Hconsignee_time2").val())).getTime(),
            end_bid_time1 = $.trim($("#HPickup_time1").val()) == '' ? '':new Date($.trim($("#HPickup_time1").val())).getTime(),
            end_bid_time2 = $.trim($("#HPickup_time2").val()) =='' ? '':new Date($.trim($("#HPickup_time2").val())).getTime();

        var page = $.trim($("#whpage").val());    
        $.ajax({
            url:COMMONURL+"/api/bidload/GetPageList?token="+ $.cookie('token'),
            type:"post",
            data:{
                bid_no:bid_no,
                ordernumber:ordernumber,
                load_state:load_state,
                consignee_time1:state_bid_time1,
                consignee_time2:state_bid_time2,
                pickup_time1:end_bid_time1,
                pickup_time2:end_bid_time2,
				"page":page,
				"pagesize":20,
				state:20
            },
            dataType:"json"
        }).done(function(redata){
            if (redata.recode == '0000' && redata.mod.Items.length>0) {
                var compiled = _.template($("#waybillHistoryTpl").html());
                var odata = redata.mod.Items;
                // alert(that.format(odata[0].start_bid_time))
                for (var i = 0, length = odata.length; i < length; i++) {
                    odata[i].start_bid_time = that.format(odata[i].start_bid_time);
                    odata[i].end_bid_time = that.format(odata[i].end_bid_time);
                    
                     odata[i].consignee_time=that.format(odata[i].consignee_time);
                }
                $("#waybillHistoryCont").html(compiled(redata.mod));
   
            }else{
                var compiled = _.template($("#waybillHistoryTpl").html());
                var odata = redata.mod.Items;
                $("#waybillHistoryCont").html(compiled(redata.mod));
                $("#wayHisdemo").hide();
            }
        });
    }

}

waybill_history.init();